#!/bin/bash

# Issue #003 验证脚本 - 测试"See Full Specs"功能缺失

set -e

echo "🔍 Issue #003: Trim Specs信息不完整验证"
echo "==========================================="

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

# 检查服务是否运行
echo "1. 检查SPAM3UK服务状态..."
if ! curl -s http://localhost:8080/health > /dev/null; then
    echo -e "${RED}❌ 服务未运行，请先启动服务${NC}"
    echo "启动命令: go run main.go 或 ./spam3uk"
    exit 1
fi
echo -e "${GREEN}✅ 服务运行正常${NC}"

# 测试获取Puma的trim信息
echo -e "\n2. 获取Puma trim信息..."
TRIM_RESPONSE=$(curl -s "http://localhost:8080/api/get_trims?model=Puma")

if [[ -z "$TRIM_RESPONSE" || "$TRIM_RESPONSE" == "null" ]]; then
    echo -e "${YELLOW}⚠️  没有找到Puma的trim数据，尝试抓取...${NC}"
    
    # 触发抓取
    echo "3. 触发trim数据抓取..."
    SCRAPE_RESPONSE=$(curl -s -X POST "http://localhost:8080/api/scrape_trims?model=Puma")
    echo "抓取响应: $SCRAPE_RESPONSE"
    
    # 再次获取
    sleep 2
    TRIM_RESPONSE=$(curl -s "http://localhost:8080/api/get_trims?model=Puma")
fi

# 分析trim数据
echo -e "\n4. 分析当前trim数据结构..."
if command -v jq &> /dev/null; then
    echo "Trim数量:"
    echo "$TRIM_RESPONSE" | jq '.trims | length'
    
    echo -e "\n第一个trim的基本信息:"
    echo "$TRIM_RESPONSE" | jq '.trims[0].trim_info | {trim_name, trim_display_name, price_from, see_full_specs_available}'
    
    echo -e "\n规格信息示例:"
    echo "$TRIM_RESPONSE" | jq '.trims[0].specifications | to_entries[0:3]' 2>/dev/null || echo "无specifications数据"
else
    echo "安装jq以获得更好的JSON分析: brew install jq"
    echo "原始响应前500字符:"
    echo "$TRIM_RESPONSE" | head -c 500
fi

# 检查规格分类的完整性
echo -e "\n5. 检查规格分类完整性..."

# 预期的规格分类
EXPECTED_CATEGORIES=("Performance" "Battery" "Charging" "Powertrain" "Dimensions" "Features" "Safety")

echo "预期的规格分类数量: ${#EXPECTED_CATEGORIES[@]}"
echo "预期分类: ${EXPECTED_CATEGORIES[*]}"

if command -v jq &> /dev/null; then
    # 检查实际的分类
    ACTUAL_CATEGORIES=$(echo "$TRIM_RESPONSE" | jq -r '.trims[0].trim_info.specifications.categories | keys[]' 2>/dev/null | sort -u)
    
    echo -e "\n实际的规格分类:"
    echo "$ACTUAL_CATEGORIES"
    
    # 统计分类数量
    ACTUAL_COUNT=$(echo "$ACTUAL_CATEGORIES" | wc -l)
    echo -e "\n实际规格分类数量: $ACTUAL_COUNT"
    
    # 检查规格数量
    SPECS_COUNT=$(echo "$TRIM_RESPONSE" | jq '.trims[0].trim_info.specs_metadata.total_specs' 2>/dev/null || echo "0")
    echo "总规格项数量: $SPECS_COUNT"
    
    # 问题判断
    echo -e "\n6. 问题分析:"
    
    if [[ "$SPECS_COUNT" -lt 20 ]]; then
        echo -e "${RED}❌ Issue #003 确认: 规格信息过少 ($SPECS_COUNT < 20)${NC}"
        echo -e "${YELLOW}   说明: 当前只获取了基础信息，缺少'See Full Specs'的详细内容${NC}"
    else
        echo -e "${GREEN}✅ 规格信息较为完整 ($SPECS_COUNT 项)${NC}"
    fi
    
    if [[ "$ACTUAL_COUNT" -lt 5 ]]; then
        echo -e "${RED}❌ Issue #003 确认: 规格分类过少 ($ACTUAL_COUNT < 5)${NC}"
        echo -e "${YELLOW}   说明: 缺少Performance、Battery、Charging等关键分类${NC}"
    else
        echo -e "${GREEN}✅ 规格分类相对完整 ($ACTUAL_COUNT 个分类)${NC}"
    fi
    
    # 检查是否有"See Full Specs"标记
    SEE_FULL_SPECS=$(echo "$TRIM_RESPONSE" | jq '.trims[0].trim_info.see_full_specs_available' 2>/dev/null)
    if [[ "$SEE_FULL_SPECS" == "true" ]]; then
        echo -e "${YELLOW}⚠️  see_full_specs_available=true 但数据不完整${NC}"
        echo -e "${YELLOW}   说明: 标记了支持'See Full Specs'但实际未实现${NC}"
    fi
fi

# 对比分析
echo -e "\n7. 与Ford官网对比分析..."
echo "Ford官网Puma Gen-E应包含的完整规格分类:"
for category in "${EXPECTED_CATEGORIES[@]}"; do
    if echo "$ACTUAL_CATEGORIES" | grep -q "$category"; then
        echo -e "  ${GREEN}✅ $category${NC}"
    else
        echo -e "  ${RED}❌ $category (缺失)${NC}"
    fi
done

# 建议的修复方案
echo -e "\n8. 修复建议:"
echo -e "${BLUE}🔧 需要实现的功能:${NC}"
echo "   1. 在trim_scraper.go中实现chromedp点击'See Full Specs'按钮"
echo "   2. 实现滚动加载等待机制，确保动态内容完全加载"
echo "   3. 解析完整的规格信息，包含所有分类"
echo "   4. 更新数据库存储逻辑以支持更详细的规格分类"

echo -e "\n${BLUE}🎯 验证方法:${NC}"
echo "   1. 手动访问: https://www.ford.co.uk/cars/puma-gen-e#models"
echo "   2. 点击任一trim的'See Full Specs'按钮"
echo "   3. 对比显示的完整信息与当前API返回的数据"

# 输出测试报告
echo -e "\n9. 测试报告摘要:"
echo "=================================="
echo "问题状态: 🔴 CONFIRMED"
echo "影响范围: Trim功能核心体验"
echo "优先级别: HIGH"
echo "修复复杂度: MEDIUM (需要chromedp交互增强)"

echo -e "\n${YELLOW}💡 临时缓解方案:${NC}"
echo "   可以在前端显示提示信息，告知用户当前为基础规格信息"
echo "   提供Ford官网链接供用户查看完整规格"

echo -e "\n测试完成! 📋 详细信息请查看 issues/003-incomplete-trim-specs-see-full-specs.md"
